package com.example.tms.controller;

import com.example.tms.bean.*;
import com.example.tms.mapper.OilcardMapper;
import com.example.tms.mapper.RechargeMapper;
import com.example.tms.tools.GlobalParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.tms.annotation.*;
/**
 * 充值信息
 */
@RestController
public class RechargeController {

    private final RechargeMapper rechargeMapper;
    private final OilcardMapper oilcardMapper;

    @Autowired
    public RechargeController(RechargeMapper rechargeMapper, OilcardMapper oilcardMapper) {
        this.rechargeMapper = rechargeMapper;
        this.oilcardMapper = oilcardMapper;
    }

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    //查询所有充值信息列表
    @RequiresPermissions("421")
    @RequestMapping(value = "recharge",method = RequestMethod.GET)
    public ReturnInfo getRechargeInfoAll(){
        return ReturnInfo.info2(rechargeMapper.findAll());
    }

    //查询充值信息列表(油卡id)
    @RequiresPermissions("421")
    @RequestMapping(value = "recharge/{ykid}",method = RequestMethod.GET)
    public ReturnInfo getRechargeInfoYk(@PathVariable String ykid){
        return ReturnInfo.info2(rechargeMapper.findByYkidAndSfczOrderByCznumAsc(ykid, true));
    }

    //查询充值信息(充值id)
    @RequiresPermissions("421")
    @RequestMapping(value = "recharge/info/{czid}",method = RequestMethod.GET)
    public RechargeInfo getRechargeInfo(@PathVariable String czid){
        return rechargeMapper.findByCzidAndSfcz(czid, true);
    }

    //新建充值信息
    @MonitorRequest
    @RequiresPermissions("422")
    @RequestMapping(value = "recharge",method = RequestMethod.POST)
    public List<Map<String,Object>> saveRechargeInfo(@RequestBody RechargeInfo rechargeInfo){
        List<Map<String,Object>>  list = new ArrayList<>();
        Map<String,Object>  map = new HashMap<>();
        //List<RechargeInfo> result = (List<RechargeInfo>) rechargeMapper.findByYkid(rechargeInfo.getYkid());

        if(rechargeInfo.getYkid().equals("")||oilcardMapper.findByYkid(rechargeInfo.getYkid())==null){
            map.put("ykid", "0");//邮卡不存在
        }
        list.add(map);
        if(map.isEmpty()){
                rechargeInfo.setCzrq(GlobalParam.getSRq());
                rechargeInfo.setCznum(rechargeMapper.findCountByYkidAndSfcz(rechargeInfo.getYkid())+1);
                rechargeInfo.setSfcz(true);
                rechargeMapper.save(rechargeInfo);
//                OilcardMgmt oilcardMgmt = oilcardMapper.findByYkidAndSfcz(rechargeInfo.getYkid(), true);
//                if(rechargeMapper.findAll()!=null){
//                    String Ljczje = rechargeMapper.sumCzje(oilcardMgmt.getYkid());
//                    oilcardMgmt.setLjczje(Ljczje);
//                }else {
//                    oilcardMgmt.setLjczje("");
//                }
//                oilcardMgmt.setDqye(mul(oilcardMgmt.getCsje(),oilcardMgmt.getLjczje(),oilcardMgmt.getLjsjcyje()));
//                oilcardMapper.saveAndFlush(oilcardMgmt);
//                logger.info("新建充值信息"+oilcardMgmt.toString());
            }


        return list;
    }

    //批量新建充值信息
    @RequiresPermissions("422")
    @RequestMapping(value = "recharges",method = RequestMethod.POST)
    public List<List<Map<String,Object>>> saveRechargeInfos(@RequestBody List<RechargeInfo> rechargeInfoList){
        List<List<Map<String,Object>>>  reslist = new ArrayList<>();
        for (RechargeInfo rechargeInfo:rechargeInfoList) {
            List<Map<String,Object>>  list = new ArrayList<>();
            Map<String,Object>  map = new HashMap<>();
            if(rechargeInfo.getYkid().equals("")||oilcardMapper.findByYkid(rechargeInfo.getYkid())==null){
            map.put("ykid", "0");//邮卡不存在
        }
            list.add(map);
            reslist.add(list);
        if(map.isEmpty()){
            rechargeInfo.setCzrq(GlobalParam.getSRq());
            rechargeInfo.setCznum(rechargeMapper.findCountByYkidAndSfcz(rechargeInfo.getYkid())+1);
            rechargeInfo.setSfcz(true);
            rechargeMapper.save(rechargeInfo);
//            OilcardMgmt oilcardMgmt = oilcardMapper.findByYkidAndSfcz(rechargeInfo.getYkid(), true);
//            if(rechargeMapper.findAll()!=null){
//                String Ljczje = rechargeMapper.sumCzje(oilcardMgmt.getYkid());
//                oilcardMgmt.setLjczje(Ljczje);
//            }else {
//                oilcardMgmt.setLjczje("");
//            }
//            oilcardMgmt.setDqye(mul(oilcardMgmt.getCsje(),oilcardMgmt.getLjczje(),oilcardMgmt.getLjsjcyje()));
//            oilcardMapper.saveAndFlush(oilcardMgmt);
            }
        }
        return reslist;
    }
    
    //更新充值信息
    @MonitorRequest
    @RequiresPermissions("423")
    @RequestMapping(value = "recharge",method = RequestMethod.PUT)
    public List<Map<String,Object>> updateRechargeInfo(@RequestBody RechargeInfo rechargeInfo){
        List<Map<String,Object>>  list = new ArrayList<>();
        Map<String,Object>  map = new HashMap<>();
        if(rechargeMapper.findByCzidAndSfcz(rechargeInfo.getCzid(), true)==null){
            map.put("czid", "0");//该充值信息不存在
        }
        if(rechargeInfo.getCzid()==null||oilcardMapper.findByYkid(rechargeInfo.getCzid())==null){
            map.put("ykid", "0");//油卡不存在
        }
        list.add(map);
        if(map.isEmpty()){
            rechargeInfo.setSfcz(true);
            rechargeInfo.setCznum(rechargeMapper.findByCzidAndSfcz(rechargeInfo.getCzid(), true).getCznum());
            rechargeMapper.saveAndFlush(rechargeInfo);
//            OilcardMgmt oilcardMgmt = oilcardMapper.findByYkidAndSfcz(rechargeInfo.getYkid(), true);
//            if(rechargeMapper.findAll()!=null){
//                String Ljczje = rechargeMapper.sumCzje(oilcardMgmt.getYkid());
//                oilcardMgmt.setLjczje(Ljczje);
//            }else {
//                oilcardMgmt.setLjczje("");
//
//            }
//            oilcardMgmt.setDqye(mul(oilcardMgmt.getCsje(),oilcardMgmt.getLjczje(),oilcardMgmt.getLjsjcyje()));
//            oilcardMapper.saveAndFlush(oilcardMgmt);
//            logger.info("更新充值信息"+oilcardMgmt.toString());
        }
        return list;
    }

    //删除充值信息（假删除 充值id）
    @MonitorRequest
    @RequiresPermissions("424")
    @RequestMapping(value = "recharge/{czid}",method = RequestMethod.DELETE)
    public ReturnInfo deleteAreaInfo(@PathVariable String czid){
        String b;
        if(rechargeMapper.findByCzidAndSfcz(czid,true)==null){
            b = "该充值信息不存在！";
        }else{
            RechargeInfo rechargeInfo = rechargeMapper.findByCzidAndSfcz(czid,true);
            int cznum = rechargeInfo.getCznum();
            String ykid = rechargeInfo.getYkid();
            rechargeMapper.updatePart(ykid,cznum);
            rechargeMapper.DeleteByCzid(czid);
            b = "删除成功！";
            logger.info("删除充值信息"+czid);
        }
        return ReturnInfo.info1(b);
    }

    //计算当前余额
    private String mul(String csje, String ljczje, String ljsjcyje){
        String dqye = "";
        if(!csje.equals("")&&!ljczje.equals("")&&!ljsjcyje.equals("")){
            BigDecimal b1 = new BigDecimal(csje);
            BigDecimal b2 = new BigDecimal(ljczje);
            BigDecimal b3 = new BigDecimal(ljsjcyje);
            dqye = b1.add(b2).subtract(b3).toString();
        }
        return dqye;
    }
}
